<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateFeeConfigAPIRequest;
use App\Http\Requests\API\UpdateFeeConfigAPIRequest;
use App\Models\FeeConfig;
use App\Repositories\FeeConfigRepository;
use App\Services\FeeServices;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class FeeConfigController
 * @package App\Http\Controllers\API
 */

class FeeConfigAPIController extends AppBaseController
{
    /** @var  FeeConfigRepository */
    private $feeConfigRepository;
    use FeeServices;

    public function __construct(FeeConfigRepository $feeConfigRepo)
    {
        $this->feeConfigRepository = $feeConfigRepo;
    }

    /**
     * Display a listing of the FeeConfig.
     * GET|HEAD /feeConfigs
     *
     * @param $Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->feeConfigRepository->pushCriteria(new RequestCriteria($request));
        $this->feeConfigRepository->pushCriteria(new LimitOffsetCriteria($request));
        $data = $this->feeConfigRepository->paginate($request->limit);
        $res = $data->toArray();
        foreach ($res['data'] as &$val) {
            $val['period_top'] = re2float($val['period_top']/100);
            $val['start_money'] = re2float($val['start_money']/100);
            $val['adders_money'] = re2float($val['adders_money']/100);
            $val['adders_time'] = ($val['adders_time']/3600);
            $val['free_time'] = ($val['free_time']/60);
            $val['start_time'] = ($val['start_time']/60);
        }
        return $this->sendResponse($res, '获取成功');
    }

    /**
     * Store a newly created FeeConfig in storage.
     * POST /feeConfigs
     *
     * @param CreateFeeConfigAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateFeeConfigAPIRequest $request)
    {
        $input = $request->all();
        $park_code = $input['park_code'];
        $input['start_money'] = $input['start_money']*100;
        $input['period_top'] = $input['period_top']*100;
        $input['adders_time'] = $input['adders_time']*3600;
        $input['start_time'] = $input['start_time']*60;
        $input['adders_money'] = $input['adders_money']*100;
        $input['free_time'] = $input['free_time']*60;
        unset($input['park_code']);
        $flag = false;
        foreach ($park_code as $v) {
            /** @var FeeConfig $feeConfig */
            $feeConfigs = $this->feeConfigRepository->updateOrCreate(['park_code'=>$v,"car_type"=>$input['car_type'],"rule"=>$input['rule']], $input);
            if($feeConfigs){
                $input['park_code'] = $v;
                $tmp = $this->send($input);
                $flag = $tmp['Result'];
            }
        }
        if($flag) {
            return $this->sendResponse($feeConfigs->toArray(), '操作成功');
        } else {
            return $this->sendResponse([], "下发失败");
        }
    }

    /**
     * Display the specified FeeConfig.
     * GET|HEAD /feeConfigs/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($park_code)
    {
        /** @var FeeConfig $feeConfig */
        $feeConfig = $this->feeConfigRepository->findWhere(['park_code'=>$park_code]);

        if (empty($feeConfig)) {
            return $this->sendError('没有此数据');
        }
        $feeConfig->start_money = $feeConfig->start_money/100;
        $feeConfig->period_top = $feeConfig->period_top/100;
        $feeConfig->adders_time = $feeConfig->adders_time/3600;
        return $this->sendResponse($feeConfig->toArray(), '获取成功');
    }

    /**
     * Remove the specified FeeConfig from storage.
     * DELETE /feeConfigs/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var FeeConfig $feeConfig */
        $feeConfig = $this->feeConfigRepository->findWithoutFail($id);

        if (empty($feeConfig)) {
            return $this->sendError('Fee Config not found');
        }

        $feeConfig->delete();

        return $this->sendResponse($id, 'Fee Config deleted successfully');
    }
}
